﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using UnityEngine;

namespace DashFire
{
  class ResPackageLoader
  {
    public static ResAsyncInfo RequestResPackage()
    {
      ResAsyncInfo info = new ResAsyncInfo();
      info.CurCoroutine = CoroutineProxy.Instance.StartCoroutine(RequestResPackageAsync(info));
      return info;
    }
    private static IEnumerator RequestResPackageAsync(ResAsyncInfo info)
    {
      if (Application.internetReachability == NetworkReachability.NotReachable) {
        ResLoadHelper.Log("RequestResPackage network not reachable:");
        info.IsError = true;
        ResUpdateControler.s_UpdateError = ResUpdateError.Network_Error;
        yield break;
      }

      ResVersionProvider.Instance.Clear();
      int clientResVersion = ResUpdateControler.s_ClientVersionInfo.Version.ResBase;
      int serverResVersion = ResUpdateControler.s_ServerVersionInfo.Version.ResBase;
      if (serverResVersion > clientResVersion) {
        LogicForGfxThread.UpdateLoadingTip("正在获取资源列表...");
        yield return 1;
        for (int curResVersion = clientResVersion + 1; curResVersion <= serverResVersion; curResVersion++) {
          string requestResPackageUrl = ResLoadHelper.GetFilePath(ResPathType.Remote,
            string.Format("{0}.{1}.{2}/{3}",
              ResUpdateControler.s_ServerVersionInfo.Version.AppChannel,
              ResUpdateControler.s_ServerVersionInfo.Version.AppMaster,
              curResVersion,
              ResUpdateConfig.s_ResVersionFile)
              );
          requestResPackageUrl = ResLoadHelper.GetDynamicUrl(requestResPackageUrl);
          ResLoadHelper.Log("RequestResPackage URL;" + requestResPackageUrl);
          using (WWW tWWW = new WWW(requestResPackageUrl)) {
            yield return tWWW;
            try {
              if (tWWW.error != null || (tWWW.text != null && tWWW.text.ToLower().Contains("<html"))) {
                ResLoadHelper.Log("RequestResPackageList package not found:" + requestResPackageUrl);
                continue; ;
              }
              byte[] buffer = tWWW.bytes;
              if (buffer == null || buffer.Length < 0) {
                ResLoadHelper.Log("RequestResPackageList tWWW.byte null");
                info.IsError = true;
                ResUpdateControler.s_UpdateError = ResUpdateError.RequestResPackage_Byte_Error;
                tWWW.Dispose();
                yield break;
              }
              ResVersionProvider.Instance.CollectDataFromDBC(buffer);
            } catch (System.Exception ex) {
              ResLoadHelper.Log("RequestResPackage ex:" + ex);
              info.IsError = true;
              ResUpdateControler.s_UpdateError = ResUpdateError.RequestResPackage_Save_Error;
            } finally {
              tWWW.Dispose();
            }
          }
        }
      } else {
        ResLoadHelper.Log("RequestResPackage no package to download");
      }

      info.IsDone = true;
      info.Progress = 1.0f;
    }
  }
}
